package com.gxa.order.service.impl;

import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.common.utils.Constants;
import com.gxa.common.utils.DateUtils;
import com.gxa.common.utils.RandomUtils;
import com.gxa.order.mapper.ServiceOrderMapper;
import com.gxa.order.pojo.*;
import com.gxa.order.service.ServiceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auth WK
 * @DATE 2022/10/7 14:50
 * @DESCRIBE
 */
@Service
public class ServiceOrderServiceImpl implements ServiceOrderService {

    @Autowired
    private ServiceOrderMapper serviceOrderMapper;

    /**
     * 1：添加房产
     * @param houseProperty
     * @return
     */
    @Override
    public R addHouseProperty(HouseProperty houseProperty) {
        R r = new R();
        try {
            int result = serviceOrderMapper.addHouseProperty(houseProperty);
            if (result > 0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 2：提交订单
     * @param cleanOrder
     * @return
     */
    @Override
    public R addCleanOrder(CleanOrder cleanOrder) {
        R r = new R();
        try {
            //生成随机订单号
            String cleanOrderNumber = RandomUtils.generateNumberString(16);
            //获取系统当前时间
            String orderTime = DateUtils.getCurrentTime();
            cleanOrder.setCleanOrderNumber(cleanOrderNumber);
            cleanOrder.setOrderTime(orderTime);
            int result = serviceOrderMapper.addCleanOrder(cleanOrder);
            if (result > 0){
                r.setCode(200);
                r.setMsg("下单成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 3：用户名下房产信息的查询
     * @param userId
     * @return
     */
    @Override
    public JsonResult findHousePropertyList(Integer userId) {
        JsonResult jsonResult = new JsonResult();
        try {
        //总条数
        int count = serviceOrderMapper.findCountOfHouseProperty(userId,Constants.STATUS_DEL);
//        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
//        Integer off = pageSize; //分页第二个参数
        List<Map> housePropertyList = serviceOrderMapper.findHousePropertyList(userId,Constants.STATUS_DEL);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setCount(count);
            jsonResult.setData(housePropertyList);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 4:用户下全部服务订单查询
     * @return
     */
    @Override
    public JsonResult findCleanOrderAll(Integer userId) {
        JsonResult jsonResult = new JsonResult();
        try {
            //总条数
            int count = serviceOrderMapper.findCountOfCleanOrder(userId);
//        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
//        Integer off = pageSize; //分页第二个参数
            List<Map> cleanOrderAll = serviceOrderMapper.findCleanOrderAll(userId);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setCount(count);
            jsonResult.setData(cleanOrderAll);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 5:用户下服务订单查询（待支付）
     * @return
     */
    @Override
    public JsonResult findCleanOrder1(Integer userId,Integer status) {
        JsonResult jsonResult = new JsonResult();
        try {
            //总条数
            int count = serviceOrderMapper.findCountOfCleanOrder1(userId,status);
//        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
//        Integer off = pageSize; //分页第二个参数
            List<Map> cleanOrder1 = serviceOrderMapper.findCleanOrder1(userId,status);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setCount(count);
            jsonResult.setData(cleanOrder1);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 6:用户下服务订单查询（服务中）
     * @return
     */
    @Override
    public JsonResult findCleanOrder2() {
        return null;
    }

    /**
     * 7:用户下服务订单查询（服务完成）
     * @return
     */
    @Override
    public JsonResult findCleanOrder3() {
        return null;
    }

    /**
     * 8:用户下服务订单查询（服务暂停）
     * @return
     */
    @Override
    public JsonResult findCleanOrder4() {
        return null;
    }

    /**
     * 9:用户下服务订单查询（点击查看详情）
     * @return
     */
    @Override
    public JsonResult findCleanOrder5(Integer cleanOrderId) {
        JsonResult jsonResult = new JsonResult();
        try {
            Map cleanOrder5 = serviceOrderMapper.findCleanOrder5(cleanOrderId);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(cleanOrder5);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 10:用户下服务订单查询（已取消）
     * @return
     */
    @Override
    public JsonResult findCleanOrder6() {
        return null;
    }

    /**
     * 11:取消订单
     * @param cleanOrderId
     * @return
     */
    @Override
    public R delCleanOrder(Integer cleanOrderId) {
        R r = new R();
        try {
            int result = serviceOrderMapper.delCleanOrder(cleanOrderId,Constants.STATUS_FOUR);
            if (result > 0){
                r.setCode(200);
                r.setMsg("取消成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 12:点击服务中订单的查看订单详情
     * @param cleanOrderId
     * @return
     */
    @Override
    public JsonResult findCleanOrderByStatus(Integer cleanOrderId) {
        return null;
    }

    /**
     * 13：编辑节点状态
     * @param scheduleStatus
     * @param cleanOrderId
     * @return
     */
    @Override
    public R updateCleanOrderStatus(Integer cleanOrderId,Integer scheduleStatus) {
        R r = new R();
        try {
            int result = serviceOrderMapper.updateCleanOrderStatus(cleanOrderId,scheduleStatus);
            if (result > 0){
                r.setCode(200);
                r.setMsg("编辑成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 14:查询员工任务
     * @param cleanOrderId
     * @return
     */
    @Override
    public JsonResult findEmployeeTaskList(Integer cleanOrderId) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<EmployeeTask> employeeTaskList = serviceOrderMapper.findEmployeeTaskList(cleanOrderId);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(employeeTaskList);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 15:修改员工任务状态
     * @param employeeTaskId
     * @return
     */
    @Override
    public R updateEmployeeTaskStatus(Integer employeeTaskId,Integer status) {
        R r = new R();
        try {
            int result = serviceOrderMapper.updateEmployeeTaskStatus(employeeTaskId,status);
            if (result > 0){
                r.setCode(200);
                r.setMsg("修改状态成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 16:添加员工任务
     * @param employeeTask
     * @return
     */
    @Override
    public R addEmployeeTask(EmployeeTask employeeTask) {
        R r = new R();
        try {
            int result = serviceOrderMapper.addEmployeeTask(employeeTask);
            if (result > 0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 17:取消任务
     * @param employeeTaskId
     * @return
     */
    @Override
    public R delEmployeeTask(Integer employeeTaskId) {
        R r = new R();
        try {
            int result = serviceOrderMapper.delEmployeeTask(employeeTaskId,Constants.STATUS_FOUR);
            if (result > 0){
                r.setCode(200);
                r.setMsg("取消成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    /**
     * 18:查询员工任务详情
     * @param employeeTaskId
     * @return
     */
    @Override
    public JsonResult findEmployeeTask(Integer employeeTaskId) {
        JsonResult jsonResult = new JsonResult();
        try {
            EmployeeTask employeeTask = serviceOrderMapper.findEmployeeTask(employeeTaskId);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(employeeTask);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 20:查询该订单下的财务信息
     * @param cleanOrderId
     * @return
     */
    @Override
    public JsonResult findFinanceList(Integer cleanOrderId) {
        return null;
    }

    /**
     * 21:查询某条财务信息的详情
     * @param financeId
     * @return
     */
    @Override
    public JsonResult findFinance(Integer financeId) {
        return null;
    }

    /**
     * 22:编辑财务信息
     * @param financeId
     * @return
     */
    @Override
    public R updateFinance(Integer financeId) {
        return null;
    }

    /**
     * 26:查询房产信息详情
     * @param housePropertyId
     * @return
     */
    @Override
    public JsonResult findHousePropertyDetails(Integer housePropertyId) {
        JsonResult jsonResult = new JsonResult();
        try {
            Map map = new HashMap();
            HouseProperty houseProperty = serviceOrderMapper.findHousePropertyDetails(housePropertyId,Constants.STATUS_DEL);
            List houseInformation = Arrays.asList(houseProperty.getHouseInformation().split(","));
            map.put("houseProperty",houseProperty);
            map.put("houseInformation",houseInformation);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(map);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 27:查询执行人
     * @return
     */
    @Override
    public JsonResult findExecutorList() {
        JsonResult jsonResult = new JsonResult();
        try {
            List<Executor> executors = serviceOrderMapper.findExecutorList();
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(executors);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 28:查询服务规格
     * @return
     */
    @Override
    public JsonResult findOrderServiceList1() {
        JsonResult jsonResult = new JsonResult();
        try {
            List<OrderService> orderService = serviceOrderMapper.findOrderServiceList1();
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(orderService);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 29：查询房间规格
     * @param orderServiceId
     * @return
     */
    @Override
    public JsonResult findRoomAreaList1(Integer orderServiceId) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<RoomArea> roomAreas = serviceOrderMapper.findRoomAreaList1(orderServiceId);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(roomAreas);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 30：根据订单编号查订单id
     * @param cleanOrderNumber
     * @return
     */
    @Override
    public JsonResult findOrderIdByNumber(String cleanOrderNumber) {
        JsonResult jsonResult = new JsonResult();
        try {
            int cleanOrderId = serviceOrderMapper.findOrderIdByNumber(cleanOrderNumber);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(cleanOrderId);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

    /**
     * 31：根据订单id编辑支付时间
     * @param cleanOrderId
     * @param payTime
     * @return
     */
    @Override
    public JsonResult updateOrderPayTime(Integer cleanOrderId,String payTime) {
        JsonResult jsonResult = new JsonResult();
        try {
            CleanOrder cleanOrder = new CleanOrder();
            cleanOrder.setCleanOrderId(cleanOrderId);
            cleanOrder.setPayTime(payTime);
            int result = serviceOrderMapper.updateOrderPayTime(cleanOrder);
            jsonResult.setCode(200);
            jsonResult.setMsg("添加成功");
            jsonResult.setData(result);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult;
    }

}
